package com.hgshop.yudongyang.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hgshop.yudongyang.domain.Cart;
import com.hgshop.yudongyang.domain.CartVo;
import com.hgshop.yudongyang.domain.HgUser;
import com.hgshop.yudongyang.domain.Sku;
import com.hgshop.yudongyang.domain.Spec;
import com.hgshop.yudongyang.domain.Spu;
import com.hgshop.yudongyang.service.SkuService;
import com.hgshop.yudongyang.service.SpecService;
import com.hgshop.yudongyang.service.SpuService;
import com.hgshop.yudongyang.service.UserService;

@Controller
public class UserController {

	@Reference
	SkuService skuService;
	@Reference
	SpuService spuService;
	@Reference
	SpecService SpecService;
	@Reference
	UserService  userService;
	
	
	//去购物车
	@RequestMapping("toCart")
	public String toCart(HttpServletRequest req,Model m) {
		
		//根据uid查询该用户下所有的购物车
				HgUser user = (HgUser) req.getSession().getAttribute("hg_user");
				if(user!=null) {
					List<Cart> carts = userService.findCartByUid(user.getUid());
					m.addAttribute("carts", carts);
				}else {
					return "redirect:toLogin";
				}
				return "cart";
	}
	
	//加入购物车
	@RequestMapping("addCart")
	@ResponseBody
	public String addCart(CartVo vo,HttpServletRequest req) {
		HgUser user= (HgUser)req.getSession().getAttribute("hg_user");
		try {
			if(user != null) {
				vo.setUid(user.getUid());
				userService.addCart(vo);
				return "success";
			}
		} catch (Exception e) {
			// TODO: handle exception
			return "fail";
		}
		//先判断是否登录，未登录条登陆页面或者提示未登录
		
		
		
		return "noLogin";
	}
	
	
	@RequestMapping("toLogin")
	public String toLogin() {
		
		return "login";
	}
	@RequestMapping("login")
	@ResponseBody
	public boolean login(HgUser user,HttpServletRequest req) {
		if(user != null) {
			HgUser dbUser = userService.findUserByUsernameAndPassword(user);
			if(dbUser!= null) {
				req.getSession().setAttribute("hg_user", dbUser);
				return true;
				
			}
			return false;
		}
		return false;
	}
	
	
	
	@RequestMapping("goodsx")
	@ResponseBody
	public List<Sku> goodsx(Integer spuId){
		List<Sku> skus = skuService.findSkuBySpuId(spuId);
		return skus;
	}
	
	//商品详情
	@RequestMapping("goodsDetail")
	public String goodsDetail(Integer spuId,Model model) {
		//根据spuId查询SKU
		List<Sku> skus = skuService.findSkuBySpuId(spuId);
		if(skus!= null&&skus.size()>0) {
			Sku sku = skus.get(0);
			List<Spec> specs = SpecService.findSpecBySkuId(sku.getId());
			model.addAttribute("specs", specs);
			
		}

		model.addAttribute("skus", skus);
		
		
		
		
		return "goodsDetail";
	}
	
	
	
	
	@RequestMapping("findSpuByCatId")
	public String findSpuByCatId(Integer catId,Model model) {
		List<Spu> spus = spuService.findSpuByCatId(catId);
		model.addAttribute("spus", spus);
		return "list";
	}
}
